package edu.chinasoft.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import edu.chinasoft.pojo.Goods;
import edu.chinasoft.util.UserLogin;

/**
 * 
 * @author cjogng
 * @date 2020年1月19日:下午2:50:50
 * @version V1.0
 * @Description: 
*/
@WebServlet(name="myAddShoppingCartForServlet",value="/servlet/addshoppingcartforservlet")
public class  AddShoppingCartForServlet  extends HttpServlet {
	private static Logger logger = Logger.getLogger( AddShoppingCartForServlet .class);
	
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" AddShoppingCartForServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		boolean result = UserLogin.isLogin(request);
		
		if(!result) {
			response.sendRedirect(""+request.getContextPath()+"/login.html");	
		}
		
		
		
		
		
		
		
		//获取所要添加商品的信息
		String goods_id = request.getParameter("goods_id");
		String goods_name = request.getParameter("goods_name");
		//封装到商品对象里
		Goods goods = new Goods();
		goods.setId(Integer.parseInt(goods_id));
		goods.setGoodsName(goods_name);
		
		/*
		 * 
		 * 购物车是一个session对象
		 * 对象结构
		 *      shop
		 *      List<Goods>
		 * 
		 */
	
		List<Goods> goodsList = null;
		
		HttpSession session = request.getSession(false);
		if(session!=null) {
			List<Goods> value = (List<Goods>)session.getAttribute("shop");
			if(value!=null) {
				goodsList=value;
			}
			
		}
		
		if(goodsList==null) {                 //第一次执行
			goodsList=new ArrayList<Goods>();       //创建list对象
			goodsList.add(goods);                   //把商品添加到list
			session = request.getSession();          //创建session对象
			session.setAttribute("shop", goodsList);    //把list添加到Session对象
			
			
			//创建JSESSIONID=9A2F650C2F9A9594F27792F29DCDE4BC; Path=/javaweb47_02;
			Cookie cookie = new Cookie("JSESSIONID",session.getId());
			
			cookie.setPath(request.getContextPath());
			cookie.setMaxAge(1*60*30);
			response.addCookie(cookie);
			
			
			
		}else {     //非第一次执行
			goodsList.add(goods);
			session.setAttribute("shop", goodsList);
		}
		
		out.write("商品:"+goods_id+"添加到购物车。");
		
		logger.debug(" AddShoppingCartForServlet >doGet end.......");
	}
	
	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" AddShoppingCartForServlet >doPost start.......");
		this.doGet(request, response);
		
		logger.debug(" AddShoppingCartForServlet >doPost end.......");
	}
	
}